﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HtmlAgilityPack;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.DAL;
using YahooFinanceWatcher.Models;

namespace YahooFinanceWatcher.Controllers
{
    public class SymbolOptionsHistoryController : Controller
    {
        private readonly ISymbolOptionsHistoryService _symbolOptionsHistoryService;

        public SymbolOptionsHistoryController(ISymbolOptionsHistoryService symbolOptionsHistoryService)
		{
            _symbolOptionsHistoryService = symbolOptionsHistoryService;
		}

        public ActionResult Index(SymbolOptionsHistoryModel model)
        {
            if (model == null)
                model = new SymbolOptionsHistoryModel();

            DataContext context = new DataContext();

            List<SqlParameter> sqlParams = new List<SqlParameter>();
            sqlParams.Add(new SqlParameter("@IsCalls", model.IsCallsFilter));
            sqlParams.Add(new SqlParameter("@DaysInTrade", model.DaysInTradeFilter));
            sqlParams.Add(new SqlParameter("@Discount", model.MinDiscountFilter));
            sqlParams.Add(new SqlParameter("@AnnualReturnField", model.MinAnnualReturnFieldFilter));
            sqlParams.Add(new SqlParameter("@AnnualReturn", model.MinAnnualReturnFilter));
            sqlParams.Add(new SqlParameter("@Probability", model.MinProbabilityFilter));

            IEnumerable<SymbolOptionsHistory> list = context.Database.SqlQuery<SymbolOptionsHistory>(@"spSearchSymbolOptionsHistory @IsCalls = @IsCalls, 
                    @DaysInTrade = @DaysInTrade, @Discount = @Discount, 
                    @AnnualReturnField = @AnnualReturnField, @AnnualReturn = @AnnualReturn,
                    @Probability = @Probability", sqlParams.ToArray()).ToList();

            if (!string.IsNullOrEmpty(model.SortOrder))
            {
                string[] sortArr = model.SortOrder.Split(',');
                if (sortArr[1] == "0")
                {
                    switch (sortArr[0])
                    {
                        case "0":
                            list = list.OrderBy(i => i.ContractName);
                            break;
                        case "1":
                            list = list.OrderBy(i => i.Symbol);
                            break;
                        case "2":
                            list = list.OrderBy(i => i.OptionDate);
                            break;
                        case "3":
                            list = list.OrderBy(i => i.SymbolPrice);
                            break;
                        case "4":
                            list = list.OrderBy(i => i.Strike);
                            break;
                        case "5":
                            list = list.OrderBy(i => i.Discount);
                            break;
                        case "6":
                            list = list.OrderBy(i => i.Bid);
                            break;
                        case "7":
                            list = list.OrderBy(i => i.Ask);
                            break;
                        case "8":
                            list = list.OrderBy(i => i.Last);
                            break;
                        case "9":
                            list = list.OrderBy(i => i.Volume);
                            break;
                        case "10":
                            list = list.OrderBy(i => i.OpenInterest);
                            break;
                        case "11":
                            list = list.OrderBy(i => i.ImpliedVolatility);
                            break;
                        case "12":
                            list = list.OrderBy(i => i.Probability);
                            break;
                        case "13":
                            list = list.OrderBy(i => i.BidAnnualReturn);
                            break;
                        case "14":
                            list = list.OrderBy(i => i.AskAnnualReturn);
                            break;
                        case "15":
                            list = list.OrderBy(i => i.BidAsk2);
                            break;
                        case "16":
                            list = list.OrderBy(i => i.LastAnnualReturn);
                            break;
                        case "17":
                            list = list.OrderBy(i => i.DaysInTrade);
                            break;

                    }
                }
                else
                {
                    switch (sortArr[0])
                    {
                        case "0":
                            list = list.OrderByDescending(i => i.ContractName);
                            break;
                        case "1":
                            list = list.OrderByDescending(i => i.Symbol);
                            break;
                        case "2":
                            list = list.OrderByDescending(i => i.OptionDate);
                            break;
                        case "3":
                            list = list.OrderByDescending(i => i.SymbolPrice);
                            break;
                        case "4":
                            list = list.OrderByDescending(i => i.Strike);
                            break;
                        case "5":
                            list = list.OrderByDescending(i => i.Discount);
                            break;
                        case "6":
                            list = list.OrderByDescending(i => i.Bid);
                            break;
                        case "7":
                            list = list.OrderByDescending(i => i.Ask);
                            break;
                        case "8":
                            list = list.OrderByDescending(i => i.Last);
                            break;
                        case "9":
                            list = list.OrderByDescending(i => i.Volume);
                            break;
                        case "10":
                            list = list.OrderByDescending(i => i.OpenInterest);
                            break;
                        case "11":
                            list = list.OrderByDescending(i => i.ImpliedVolatility);
                            break;
                        case "12":
                            list = list.OrderByDescending(i => i.Probability);
                            break;
                        case "13":
                            list = list.OrderByDescending(i => i.BidAnnualReturn);
                            break;
                        case "14":
                            list = list.OrderByDescending(i => i.AskAnnualReturn);
                            break;
                        case "15":
                            list = list.OrderByDescending(i => i.BidAsk2);
                            break;
                        case "16":
                            list = list.OrderByDescending(i => i.LastAnnualReturn);
                            break;
                        case "17":
                            list = list.OrderByDescending(i => i.DaysInTrade);
                            break;

                    }
                }
            }

            model.Hystory = list.ToList();

            if (Request.IsAjaxRequest())
                return PartialView("SymbolOptionsHistoryList", model);

            return View(model);
        }

    }
}
